﻿using IP扫描.Events;
using IP扫描.Models;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace IP扫描.ViewModels
{
    internal class IPV4ScanViewModel : BindableBase, INavigationAware
    {
        private ButtonState startButtonState;

        public ButtonState StartButtonState
        {
            get { return startButtonState; }
            set { startButtonState = value;RaisePropertyChanged(); }
        }
        private ButtonState stopButtonState;

        public ButtonState StopButtonState
        {
            get { return stopButtonState; }
            set { stopButtonState = value; RaisePropertyChanged(); }
        }

        private int progressValue;

        public int ProgressValue
        {
            get { return progressValue; }
            set { progressValue = value; RaisePropertyChanged(); }
        }

        private IPClass iPClass;

        public IPClass IPClass
        {
            get { return iPClass; }
            set { iPClass = value; RaisePropertyChanged(); }
        }


        public DelegateCommand StartCommand { get; set; }
        public DelegateCommand StopCommand { get; set; }

        //取消标记
        public CancellationTokenSource CancelTokenSource { get; set; } = new CancellationTokenSource();

        private ObservableCollection<IP256ListClass> ipList;

        public ObservableCollection<IP256ListClass> IPList
        {
            get { return ipList; }
            set { ipList = value;RaisePropertyChanged(); }
        }

        public IPV4ScanViewModel(IEventAggregator eventAggregator)
        {
            IPList = new ObservableCollection<IP256ListClass>();
            StartButtonState = new ButtonState();
            StopButtonState = new ButtonState();
            for (int i = 0; i < 256; i++)
            {
                IPList.Add(new IP256ListClass() { IPName = i });
            }

            eventAggregator.GetEvent<MessengerEvent>().Subscribe(x =>
            {
                IPClass = x;
                IPClass.N4 = "1";
            });

            StartCommand = new DelegateCommand(async () =>
            {
                StartButtonState.Loading = "Visible";
                StartButtonState.Enable = false;
                CancelTokenSource = new CancellationTokenSource();   
                int i = 0;
                ParallelOptions option = new ParallelOptions()
                {
                    CancellationToken = CancelTokenSource.Token,
                    MaxDegreeOfParallelism = Environment.ProcessorCount
                };
               await Task.Run(() =>
                {
                    try
                    {
                        Parallel.ForEach(IPList, option, ip =>
                        {
                            ip.IsEnableOfIP = new IPScanOperation().IPScan($"{IPClass.N1}.{IPClass.N2}.{IPClass.N3}.{ip.IPName}");
                            Interlocked.Add(ref i, 1);
                            ProgressValue = i;
                        });
                    }
                    catch (Exception)
                    {

                    }
                   
                });

                if (i<256)
                {
                    MessageBox.Show($"{i} -> 任务结束");
                    StartButtonState.Enable = true;
                    StartButtonState.Loading = "Collapsed";
                    return;
                }
                MessageBox.Show(i.ToString());
                StartButtonState.Enable = true;
                StartButtonState.Loading = "Collapsed";
            });

            StopCommand = new DelegateCommand(() =>
            {
                StopButtonState.Loading = "Visible";
                StopButtonState.Enable = false;
                CancelTokenSource.Cancel();
                IPList = new ObservableCollection<IP256ListClass>();

                for (int i = 0; i < 256; i++)
                {
                    IPList.Add(new IP256ListClass() { IPName = i });
                }
                StopButtonState.Loading = "Collapsed";
                StopButtonState.Enable = true;
            });
        }
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {

        }
    }
}
